package es.capgemini.extjsplugin.core;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import org.eclipse.jdt.core.IPackageFragment;

import es.capgemini.extjsplugin.ui.ExtendedField;
import es.capgemini.extjsplugin.wizard.GeneratorWizardModel;

/**
 * Clase para generar componentes Dao (interface & implementacion)
 * @author Lisandro Medrano
 *
 */
public class DaoGenerator extends JavaFileGenerator {
	
	public DaoGenerator(GeneratorWizardModel model) {
	    this.package_url = model.getPackage_url();
	    this.package_name = model.getPackage_name();
	    this.package_base = model.getPackage_url();
	    this.class_name=model.getMyClass().getSimpleName();
	    this.camposIncluir=model.getCamposIncluir();
	    this.packageFragment=model.getPackageFragment();
	    crearDirectorios();
	}
	/**
     * Crea el Dao .java
     */
    public void crearDao(String class_name) {
        File file = null;

        file = new File(new File(DAO_URL) + "/" + class_name + "Dao.java");
        try {
            file.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        PrintWriter out = null;
        try {
            out = new PrintWriter(new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.println("package " + package_name + ".dao;");
        out.println("/**");
        out.println(" * Creado el " + new Date());
        out.println(" * @author: Lisandro Medrano");

        out.println(" */");
        out.println("public interface " + class_name + "Dao extends AbstractDao<" + class_name + ", Long>" + " {");

        out.println("}");
        out.flush();
        out.close();

    }

    public void crearDaoImpl(String class_name) {
        File file = null;

        file = new File(new File(DAO_URL) + "/impl/" + class_name + "DaoImpl.java");
        try {
            file.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        PrintWriter out = null;
        try {
            out = new PrintWriter(new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.println("package " + package_name + ".dao.impl;");
        out.println("/**");
        out.println(" * Creado el " + new Date());
        out.println(" * @author: Lisandro Medrano");
        out.println(" */");
        out.println("@Repository(\"" + class_name + "Dao\")");
        out.println("public class " + class_name + "DaoImpl extends AbstractEntityDao<" + class_name + ", Long> implements  " + class_name + "Dao {");

        out.println("}");
        out.flush();
        out.close();

    }    
}
